var  e = !1;
var app = getApp(), api = getApp().api;

Page({
  data: {
    page: 0,
    page_count: 1,
    longitude: "",
    latitude: "",
    keyword: "",
    myAddress: '',

    distance:-1,
    district_id:0,
    city_id:0,
    sort_type:0,
    mch_common_cat_id:0,
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    currentTab: 0,
    click:false,
    nearby:true,
    mask:false,
    winHeight: 0,

    list:[],
    city: {},
    district_list: [],
    address: {},
    peisong_type:{
      express_delivery:0,
      self_delivery:0,
      waimai_delivery:0
    },
    peisong_type_count:0,

    storecats: '店铺分类',
    intellectsort: '智能排序',
    nearbys: '附近',
    screen: '筛选',
    district:[],
    district_index: -1,
    district_city_index: -1
  },
  onLoad: function (t) {
    var thst = this;
    getApp().pageOnLoad(this, t);
    if (this.data.store.is_shenhe == 1) {
      wx.setNavigationBarTitle({
        title: ''
      })
    }
    if (t.shop) {
      this.setData({
        shop: JSON.parse(t.shop),
      });
    }
    var e = this, o = t.user_id;
     wx.getLocation({
      success: function (t) {
        console.log(t)
        e.setData({
          longitude: t.longitude,
          latitude: t.latitude
        });
        thst.locationContent();
      },
      complete: function () {
        e.loadData();
      }
    });
   
  },
  onReady: function () { },
  onShow: function () {
    getApp().page.onShow(this);
    this.getDistrictData();
  },
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab == e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
      })
    }
  },

  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  click: function (e){
    var current_id = e.currentTarget.dataset.current_id;
    this.setData({
      currentTab:current_id,
      click:true,
      mask:true
    })
  },
  close: function (){
    this.setData({
      click:false,
      mask:false,
    })
  },

  loadData: function () {
    var o = this, n = o.data.page;
    e || (e = !0, wx.showLoading({
      title: "加载中",
    }), wx.request({
      url: getApp().api.mch.mch_list,
      method: "GET",
      data: {
        distance: o.data.distance,
        district_id: o.data.district_id,
        city_id: o.data.city_id,
        sort_type: o.data.sort_type,
        mch_common_cat_id: o.data.mch_common_cat_id,
        keyword: o.data.keyword,
        page: n,
        longitude: o.data.longitude,
        latitude: o.data.latitude,
        peisong_type: o.data.peisong_type,
      },
      success: function (t) {
        t = t.data;
        if (0 == t.code) {
          var a = o.data.list.concat(t.data.list);
          o.setData({
            banner: t.data.banner,
            store_cat: t.data.store_cat,
            list: a,
            page_count: t.data.page_count,
            row_count: t.data.row_count,
            page: n + 1
          });
        }
      },
      complete: function () {
        wx.hideLoading(), e = !1;
      }
    }));
   
  },
  onHide: function () { },
  onUnload: function () { },
  onPullDownRefresh: function () {
    var t = this;

    t.setData({
      keyword: "",
      district_id: 0,
      click:false,
      nearby:false,
      city_id:0,
      distance:-1,
      page: 0,
      list:[]
    }), wx.getLocation({
      success: function (a) {
        t.setData({
          longitude: a.longitude,
          latitude: a.latitude
        });
      },
      complete: function () {
        t.loadData(), wx.stopPullDownRefresh();
      }
    });
  },
  onReachBottom: function () {
    var t = this;
    t.data.page >= t.data.page_count || t.loadData();
  },

  locationContent: function () {
    var o = this;
    wx.request({
      url: getApp().api.default.get_location,
      method: "GET",
      data: {
        longitude: o.data.longitude,
        latitude: o.data.latitude
      },
      success: function (t) { console.log(t);
        o.setData({
          myAddress: t.data.city ? t.data.city : '选择市',
          city: t.data.city,
        })
      },
      fail: function (t) {
        o.setData({
          myAddress: "定位失败2",
        })
      },
    });
  },
 
  inputFocus: function (t) {
    this.setData({
      show: !0
    });
  },
  inputBlur: function (t) {
    this.setData({
      show: !1
    });
  },
  inputConfirm: function (t) {
    this.search();
  },
  input: function (t) {
    this.setData({
      keyword: t.detail.value
    });
  },
  search: function (e) {
      this.setData({
        click:false,
        mask:false,
        page: 0,
        list:[]
      });
      this.loadData();
  },
  searchCat: function (e) {
    var mch_common_cat_id = e.currentTarget.dataset.mch_common_cat_id;
     var storecats =  e.currentTarget.dataset.cat_name;
     this.setData({
      storecats: storecats,
      mch_common_cat_id: mch_common_cat_id,
      click:false,
      mask:false,
      page: 0,
      list:[]
     });
     this.loadData(); 
  },
  searchSortType: function (e){
    var sort_type = e.currentTarget.dataset.sort_type;
    var intellectsort = e.currentTarget.dataset.intellectsort;
    this.setData({
      intellectsort: intellectsort,
      sort_type: sort_type,
      click:false,
      mask:false,
      page: 0,
      list:[]
    });
    this.loadData(); 
  },
  searchCity: function (e) {
    var city_id = this.data.city.id;
    var nearbys =  e.currentTarget.dataset.nearbys;
    this.setData({
      nearbys: nearbys,
      city_id: city_id,
      click:false,
      mask:false,
      nearby:false,
      district_id:0,
      distance:-1,
      page: 0,
      list:[]
    });
    this.loadData();
  },
  searchDistrict: function (e) {
    var district_id = e.currentTarget.dataset.district_id;
    var nearbys =  e.currentTarget.dataset.nearbys;
    this.setData({
      nearbys: nearbys,
      district_id: district_id,
      click:false,
      mask:false,
      nearby:false,
      city_id:0,
      distance:-1,
      page: 0,
      list:[]
    });
    this.loadData();
  },
  searchDistance: function (e) {
    var distance = e.currentTarget.dataset.distance;
    var nearbys =  e.currentTarget.dataset.nearbys;
    this.setData({
      nearbys: nearbys,
      distance: distance,
      click:false,
      mask:false,
      city_id:0,
      district_id:0,
      page: 0,
      list:[]
    });
    this.loadData();
  },
  searchNearby: function (e) {
    this.setData({
      nearby:true,
      city_id:0,
      district_id:0
    });
  },
  mchService: function (e) {
    var delivery = e.currentTarget.dataset.delivery;
    var peisong_type = this.data.peisong_type;

    peisong_type[delivery] = !peisong_type[delivery];
   
    var  peisong_type_count = 0;
 
    if(peisong_type.express_delivery==1){
      peisong_type_count = peisong_type_count +1;
    }
    if(peisong_type.self_delivery==1){
      peisong_type_count = peisong_type_count +1;
    }
    if(peisong_type.waimai_delivery==1){
      peisong_type_count = peisong_type_count +1;
    }

    this.setData({
      peisong_type: peisong_type,
      peisong_type_count:peisong_type_count
    });
  },
  ChooseSubmit: function (e){
    this.setData({
      page: 0,
      list:[], 
      click:false,
      mask:false,
    });
    this.loadData();
  },
  ChooseClear: function (e) {
    var peisong_type ={
      express_delivery:0,
      self_delivery:0,
      waimai_delivery:0
    }
    this.setData({
      peisong_type: peisong_type,
      peisong_type_count:0
    });
  },
  ChoiceShop: function (e) {
    var mch_id = e.currentTarget.dataset.mch_id;
  
    wx.navigateTo({
      url: "/mch/shop/shop?mch_id=" + mch_id
    });
  },
  ChoiceGoods: function (e) {
    var goods_id = e.currentTarget.dataset.goods_id;
  
    wx.navigateTo({
      url: "/pages/goods/goods?id=" + goods_id
    });
  },
  
  onShareAppMessage: function (t) {
    return {
      path: "/pages/shop-list/shop-list?user_id=" + wx.getStorageSync("user_info").id
    };
  },
 
  getLocation: function (e) {
    var item = e.target.dataset.item;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: parseFloat(item.latitude),//要去的纬度-地址
          longitude: parseFloat(item.longitude),//要去的经度-地址
          name: item.name,
          address: item.address,
          fail: function(all){
            console.log(all);
          }
        })
      }
    })
  },
  getDistrictData: function(t) {
    var i = wx.getStorageSync('district'),that = this;;
    if (!i) return wx.showLoading({
        title: "正在加载",
        mask: !0
    }), void wx.request({
        url: getApp().api.default.district,
        success: function(e) {
          e = e.data
            wx.hideLoading(), 0 == e.code && (i = e.data, wx.setStorageSync('district', i),that.setData({
              district:i
            }))
        }
    });
    this.setData({
      district:i
    })
  },
  updateProvince: function (e) {
    var index = e.target.dataset.index;
    var city_list = this.data.district[index].list
    this.setData({
      district_index: index,
      district_city_index: -1,
      city_list: city_list
    })
  },
  updateCity: function (e) {
    var that = this;
    var index = e.target.dataset.index;
    var city_id = this.data.city_list[index].id;
    this.setData({
      city_id: city_id,
      click:false,
      mask:false,
      nearby:false,
      distance:-1,
      page: 0,
      list:[],
      myAddress: that.data.city_list[index].name,
      district_list: that.data.city_list[index].list,
      district_id: 0,
      nearbys: '附近',
    });
    this.loadData();  
  },
});
